package com.wzt.lianfirecontrol.adapter.home;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.home.ChartsModel2;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.view.MyMarkerView;

import java.util.ArrayList;

public class HomeStatisticAdapter extends BaseRecyclerAdapter<ChartsModel2> {

    private Context context;

    private boolean isDetail;

    public boolean isDetail() {
        return isDetail;
    }

    public void setDetail(boolean detail) {
        isDetail = detail;
    }

    public HomeStatisticAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_statistic2, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, ChartsModel2 data) {
        setItemData((MyViewHolder)viewHolder, data);
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_title;
        private ImageView iv_title;
        private TextView tv_title;
        private LineChart lineChart;
        private TextView tv_count1;
        private TextView tv_count2;
        private TextView tv_count3;
        private LinearLayout ll;
        private LinearLayout ll2;
        private TextView tv1;
        private TextView tv2;
        private TextView tv3;
        private TextView tv4;
        private TextView tv5;
        private TextView tv_sumCount;
        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_title = itemView.findViewById(R.id.ll_title);
        myViewHolder.iv_title = itemView.findViewById(R.id.iv_title);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.lineChart = itemView.findViewById(R.id.lineChart);
        myViewHolder.tv_count1 = itemView.findViewById(R.id.tv_count1);
        myViewHolder.tv_count2 = itemView.findViewById(R.id.tv_count2);
        myViewHolder.tv_count3 = itemView.findViewById(R.id.tv_count3);
        myViewHolder.tv1 = itemView.findViewById(R.id.tv1);
        myViewHolder.tv2 = itemView.findViewById(R.id.tv2);
        myViewHolder.tv3 = itemView.findViewById(R.id.tv3);
        myViewHolder.tv4 = itemView.findViewById(R.id.tv4);
        myViewHolder.tv5 = itemView.findViewById(R.id.tv5);
        myViewHolder.tv_sumCount = itemView.findViewById(R.id.tv_sumCount);
        myViewHolder.ll= itemView.findViewById(R.id.ll);
        myViewHolder.ll2= itemView.findViewById(R.id.ll2);
        // no description text
        myViewHolder.lineChart.getDescription().setEnabled(false);
        // enable touch gestures
        myViewHolder.lineChart.setTouchEnabled(true);
        // enable scaling and dragging
        myViewHolder.lineChart.setDragEnabled(true);
        myViewHolder.lineChart.setScaleEnabled(true);
        // if disabled, scaling can be done on x- and y-axis separately
        myViewHolder.lineChart.setPinchZoom(false);

        myViewHolder.lineChart.setDrawGridBackground(false);
        myViewHolder.lineChart.setMaxHighlightDistance(300);



        myViewHolder.lineChart.getAxisRight().setEnabled(false);

        myViewHolder.lineChart.getLegend().setEnabled(false);

        myViewHolder.lineChart.animateXY(0, 800);
        myViewHolder.lineChart.setScaleYEnabled(false);

        XAxis x = myViewHolder.lineChart.getXAxis();
        x.setTextColor(context.getResources().getColor(R.color.content_text_color));
        x.setEnabled(true);
        x.setDrawGridLines(false);
        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setAxisLineColor(Color.WHITE);
        x.setGranularityEnabled(false);
        x.setGranularity(1);

        YAxis y = myViewHolder.lineChart.getAxisLeft();
        Typeface tfLight = Typeface.DEFAULT;
        y.setTypeface(tfLight);
        y.setTextColor(context.getResources().getColor(R.color.content_text_color));
        y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        y.setDrawGridLines(true);
        y.setAxisLineColor(Color.WHITE);
        y.setGranularityEnabled(false);
        y.setGranularity(1);
        y.setAxisMinimum(0f);

        // don't forget to refresh the drawing
        myViewHolder.lineChart.invalidate();
    }

    private void setItemData(MyViewHolder viewHolder, ChartsModel2 data) {
        if (StringUtils.isEmpty(data.getChartTitle())){
            viewHolder.ll_title.setVisibility(View.GONE);
        }else {
            viewHolder.ll_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getChartTitle());
            if (isDetail){
                viewHolder.iv_title.setVisibility(View.GONE);
            }else {
                viewHolder.iv_title.setVisibility(View.VISIBLE);
            }
        }
        int position = viewHolder.getPosition();
        if ( position == 0 ){ // 火警趋势
            if (data.getFireAlarmCount()!= null) {
                viewHolder.ll2.setVisibility(View.VISIBLE);
                viewHolder.tv_sumCount.setText(data.getFireAlarmCount().getCount().getSumCount());
                viewHolder.tv_count1.setText(data.getFireAlarmCount().getCount().getCount1());
            }else {

            }

        //    viewHolder.tv_count3.setText(data.getFireAlarmCount().getCount().getCount2()+"");
        } else if ( position == 1){  // 预警趋势
            if (data.getEarlyAlarmCount()!=null) {
                viewHolder.ll2.setVisibility(View.VISIBLE);
                viewHolder.tv_sumCount.setText(data.getEarlyAlarmCount().getCount().getSumCount());
                viewHolder.tv_count1.setText(data.getEarlyAlarmCount().getCount().getCount1());
            }


        }else if ( position == 2 ){  // 故障趋势
            if (data.getRiskAlarmCount()!=null) {
                viewHolder.ll2.setVisibility(View.VISIBLE);
                viewHolder.ll.setVisibility(View.VISIBLE);
                viewHolder.tv4.setVisibility(View.VISIBLE);
                viewHolder.tv_count3.setVisibility(View.VISIBLE);
                viewHolder.tv5.setVisibility(View.VISIBLE);
                viewHolder.tv1.setText("总隐患");
                viewHolder.tv2.setText("已整改");
                viewHolder.tv3.setText(",未整改");
                viewHolder.tv4.setText(",整改中");
                int i =  Integer.valueOf(data.getRiskAlarmCount().getCount().getSumCount()).intValue();
                int i1 = Integer.valueOf(data.getRiskAlarmCount().getCount().getCount1()).intValue();
                int i2 = Integer.valueOf(data.getRiskAlarmCount().getCount().getCount2()).intValue();
                int i3 = i - i1 - i2 ;
                viewHolder.tv_sumCount.setText(data.getRiskAlarmCount().getCount().getSumCount());
                viewHolder.tv_count1.setText(data.getRiskAlarmCount().getCount().getCount1());
                viewHolder.tv_count2.setText(data.getRiskAlarmCount().getCount().getCount2());
                viewHolder.tv_count3.setText(i3 +"");
            }

        }else if ( position == 3 ){  // 监管趋势
            if (data.getSuperviseAlarmCount()!=null) {
                viewHolder.ll2.setVisibility(View.VISIBLE);
                viewHolder.ll.setVisibility(View.VISIBLE);
                viewHolder.tv4.setVisibility(View.GONE);
                viewHolder.tv_count3.setVisibility(View.GONE);
                viewHolder.tv5.setVisibility(View.GONE);
                viewHolder.tv1.setText("报警");
                viewHolder.tv2.setText("已处理");
                viewHolder.tv3.setText(",未处理");
                viewHolder.tv_sumCount.setText(data.getSuperviseAlarmCount().getCount().getSumCount());
                viewHolder.tv_count1.setText(data.getSuperviseAlarmCount().getCount().getCount1());
                viewHolder.tv_count2.setText(data.getSuperviseAlarmCount().getCount().getCount2());
            }
        }

        XAxis x = viewHolder.lineChart.getXAxis();
        if (data.getxAxis().size() > 7){
            x.setLabelCount(7, false);
        }else {
            x.setLabelCount(data.getxAxis().size());
        }
        DayAxisValueFormatter dayAxisValueFormatter = new DayAxisValueFormatter(data);
        x.setValueFormatter(dayAxisValueFormatter);

        YAxis y = viewHolder.lineChart.getAxisLeft();
        if (data.getyAxis().size() > 7){
            y.setLabelCount(7, false);
        }else {
            y.setLabelCount(data.getyAxis().size(), false);
        }
        // create a custom MarkerView (extend MarkerView) and specify the layout
        // to use for it
        MyMarkerView mv = new MyMarkerView(context, R.layout.custom_marker_view, data, isDetail);
        mv.setChartView(viewHolder.lineChart); // For bounds control
        viewHolder.lineChart.setMarker(mv); // Set the marker to the chart

        setChartData(viewHolder.lineChart, data);
    }

    private void setChartData(final LineChart chart1, ChartsModel2 chartsModel){
        ArrayList<Entry> values = new ArrayList<>();

        for (int i = 0; i < chartsModel.getxAxis().size(); i++) {
            float val = Float.parseFloat(chartsModel.getyAxis().get(i));
            values.add(new Entry(i, val));
        }

        LineDataSet set1;

        if (chart1.getData() != null &&
                chart1.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart1.getData().getDataSetByIndex(0);
            set1.setValues(values);
            chart1.getData().notifyDataChanged();
            chart1.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(values, "DataSet 1");

            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            set1.setCubicIntensity(0.2f);
            set1.setDrawFilled(true);
            set1.setDrawCircles(false);
            set1.setCircleHoleColor(context.getResources().getColor(R.color.main_color));
            set1.setLineWidth(1.8f);
            set1.setCircleRadius(4f);
            set1.setCircleColor(context.getResources().getColor(R.color.main_color));
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setColor(context.getResources().getColor(R.color.main_color));
            set1.setFillColor(context.getResources().getColor(R.color.main_color));
            set1.setFillAlpha(50);
            set1.setDrawHorizontalHighlightIndicator(true);
            set1.setFillFormatter(new IFillFormatter() {
                @Override
                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                    return chart1.getAxisLeft().getAxisMinimum();
                }
            });

            // create a data object with the data sets
            LineData data = new LineData(set1);
            data.setValueTypeface(Typeface.DEFAULT);
            data.setValueTextSize(9f);
            data.setDrawValues(false);
            data.setHighlightEnabled(true);
            // set data
            chart1.setData(data);
        }
    }

    public class DayAxisValueFormatter extends ValueFormatter {

        private ChartsModel2 chartsModel;
        private boolean isX = true;

        public DayAxisValueFormatter(ChartsModel2 chartsModel) {
            this.chartsModel = chartsModel;
        }

        public DayAxisValueFormatter(ChartsModel2 chartsModel, boolean isX) {
            this.chartsModel = chartsModel;
            this.isX = isX;
        }

        @Override
        public String getFormattedValue(float value) {
            int days = (int) value;
            if (isDetail){
                if (days >= 0 && days < chartsModel.getxAxis().size()) {
                    String str = chartsModel.getxAxis().get(days).substring(11, 16);
                    return str;
                }
            }else {
                if (days >= 0 && days < chartsModel.getHomeTendModels().size()) {
                    return chartsModel.getHomeTendModels().get(days).getWarnTime().substring(5, 10);
                }
            }
            return days+"";

        }

    }
}
